﻿using Forum.Model.Entity;
using Forum.Model.Pager;
using Forum.Model.Request;
using Forum.Model.Response;
using Forum.Repository.Interface;
using Forum.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace Forum.Service.Service
{
    public class CategoryService : ICategoryService
    {
        private readonly IAppRepository<Category> _cRepo;
        private readonly IHttpContextAccessor _accessor;
        private readonly LinkGenerator _generator;

        public CategoryService(IAppRepository<Category> cRepo,
                IHttpContextAccessor accessor,
                LinkGenerator generator)
        {
            _cRepo = cRepo;
            _accessor = accessor;
            _generator = generator;
        }

        public List<CategoryNode>? GetNodes(string name, int parentId = 0)
        {
            var categories = _cRepo.SugarQueryable
                .Where(a => a.ParentId == parentId && a.Visible)
                .Includes(a => a.Posts)
                .ToList();

            if (categories.Count == 0) return null;

            return categories.Select(category => new CategoryNode
            {
                text = category.Name,
                href = _generator.GetUriByAction(
                    _accessor.HttpContext!,
                    name,
                    "Blog",
                    new { categoryId = category.Id }
                ),
                tags = new List<string> { category.Posts.Count.ToString() },
                nodes = GetNodes(name, category.Id)
            }).ToList();
        }

        public List<Category> GetAll()
        {
            return _cRepo.GetList();
        }

        public IPagedList<Category> GetPagedList(int page = 1, int pageSize = 10)
        {
            return _cRepo.GetListByPage(null, a => a.Id, false, page, pageSize);
        }

        public Category? GetById(int id)
        {
            return _cRepo.FindEntity(a => a.Id == id);
        }

        /// <summary>
        /// 生成分类词云数据
        /// </summary>
        /// <returns></returns>
        public List<object> GetWordCloud()
        {
            var list = _cRepo.SugarQueryable
                .Where(a => a.Visible && a.ParentId == 0)
                .Includes(a => a.Posts).ToList();
            var data = new List<object>();
            foreach (var item in list)
            {
                data.Add(new { name = item.Name, value = item.Posts.Count });
            }

            return data;
        }
        
        /// <summary>
        /// 设置分类是否可见
        /// </summary>
        /// <param name="category"></param>
        /// <param name="isVisible"></param>
        /// <returns></returns>
        public bool SetVisibility(Category category, bool isVisible)
        {
            category.Visible = isVisible;
            return _cRepo.Update(category);
        }

        public bool Delete(int id)
        {
            return _cRepo.Delete(a => a.Id == id);
        }

        public Category Insert(Category category)
        {
            _cRepo.Insert(category);
            return category;
        }

        public Category Update(Category category)
        {
            _cRepo.Update(category);
            return category;
        }
    }
}
